<?php
/**
 * wiki中间页
 * Class MiddleListAction
 */

use Biz\service\wiki\WikiMiddle;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient as tHhzClient;
use Doraemon\tools\IdParse;


class MiddleListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    const IS_LOG_EXPOSE = 1;

    public function main()
    {
        $list = [];
        try {
            //每页多少数据
            $per_page = (int)$this->getRequest()->getPost('per_page', 6);
            //当前页
            $page = (int)$this->getRequest()->getPost('page', 1);
            $page = ($page > 1) ? $page : 1;
            //访问类型 普通:0
            $brand_id = (int)$this->getRequest()->getPost('brand_id', 0);
            //获取wiki_id集合
            $wiki_id = (int)$this->getRequest()->getPost('wiki_id', 0);

            $act_from = $this->getRequest()->getPost('act_from', '');//可以自定义。wiki_detail：wiki详情；brand：wiki详情品牌模块 ；
            //note动态详情页 note_detail
            //个人主页动态feed流 personal_feed
            //话题详情页热门/最新流 topic_feed
            //首页关注流 follow_feed

            $objId = $this->getRequest()->getPost('obj_id', '');

            if ($wiki_id <= 0) {
                throw new \Exception('wiki参数错误');
            }
            $uid = $this->uid;
            if ($brand_id) {
                $wiki_ids = $this->getBrandWikiLists($wiki_id, $brand_id);
                $list['brand_info'] = WikiMiddle::getBrandInfo($brand_id);
            } else {
                $wiki_ids = $this->getWikiLists($wiki_id, $uid, $page);
            }
            //总条数
            $count = count($wiki_ids);
            //偏移量，当前页-1乘以每页显示条数
            $start = ($page - 1) * $per_page;
            //每页的wiki_id集合
            $wiki_ids_page = array_slice($wiki_ids, $start, $per_page);

            $list['total'] = $count ?? 0;
            $list['per_page'] = $per_page;
            $list['current_page'] = $page;

            $params = [
                'wiki_ids' => $wiki_ids_page,
                'uid' => $uid
            ];
            $data = WikiMiddle::getMiddleWikiList($params);

            //埋点
            $index = $start;
            foreach ($data as $key => $v) {
                $extend[basicStatSign::EXTEND_KEY_OBJ_ID] = $key;
                $extend[basicStatSign::EXTEND_KEY_OBJ_TYPE] = BlockType::TYPE_WIKI;
                $extend[basicStatSign::EXTEND_KEY_INDEX] = $index;
                $extend[basicStatSign::EXTEND_KEY_RELATE_OBJ_ID] = $objId ?? '';
                $extend[basicStatSign::EXTEND_KEY_RELATE_OBJ_TYPE] = $objId ? IdParse::getType($objId) : '';
                $data[$key]['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_WIKI_MIDDLE_CARD, $extend,
                    ['brand_id' => $v['brand_id'], 'act_from' => $act_from, 'obj_id' => $objId, 'obj_type' => $objId ? IdParse::getType($objId) : '']);
                $index++;
            }

            $current = $page * $per_page;

            $list['is_over'] = ($count <= $current) ? 1 : 0;
            $list['list'] = array_values($data);

            if (tHhzClient::$client_type == tHhzClient::APPTYPE_IOS || tHhzClient::$client_type == tHhzClient::APPTYPE_ANDROID) {
                $list['list'] = \Doraemon\tools\StatSign::versionCompatible($list['list'], \Doraemon\tools\StatSign::WIKI_UP_GRADE_VERSION_522, \Doraemon\tools\BlockType::TYPE_WIKI, 'wiki');
            } else {
                tHhzClient::$appVersion = '1';
                $list['list'] = \Doraemon\tools\StatSign::versionCompatible($list['list'], 0, \Doraemon\tools\BlockType::TYPE_WIKI, 'wiki');
            }


        } catch (\Exception $e) {
            return $this->setSuccess([], self::API_CODE_ERROR, $e->getMessage());
        }
        $this->setSuccess($list);
    }

    /**
     * 获取品牌中间页列表
     * @param $wiki_id
     * @param $brand_id
     * @return array|bool
     */
    private function getBrandWikiLists($wiki_id, $brand_id)
    {
        return WikiMiddle::getBrandWikiBasicIds($wiki_id, $brand_id);
    }


    /**
     * 获取非品牌中间页列表
     * @param $wiki_id
     * @param $uid
     * @return array
     */
    private function getWikiLists($wiki_id, $uid, $page): array
    {
        $type = ($page <= 1) ? 1 : 0;
        return WikiMiddle::getWikiIds($wiki_id, $uid, $type);
    }

}